<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/css/public.css" media="all">
</head>
<body>
<div class="x-body">
    <div class="layuimini-container">
        <div class="layuimini-main">
            <div class="layui-row">
                <form class="layui-form layui-col-md12 x-so">
                    <div class="layui-input-inline">
                        <select name="carBrand">
                            <option value="0">--请选择车品牌--</option>
                            <option value="1">宝马</option>
                            <option value="2">奔驰</option>
                        </select>
                    </div>
                    <div class="layui-input-inline">
                        <select name="carType">
                            <option value="0">--请选择车型号--</option>
                            <option value="1">SUV</option>
                            <option value="2">MPV</option>
                            <option value="3">CDV</option>
                        </select>
                    </div>
                    <div class="layui-input-inline">
                        <button class="layui-btn" lay-submit lay-filter="search"><i
                                class="layui-icon">&#xe615;</i><span>搜索</span></button>
                    </div>
                    <div class="layui-input-inline">
                        <button class="layui-btn layui-btn-warm" lay-submit lay-filter="findAll"><i
                                class="layui-icon">&#xe615;</i><span>查询全部</span>
                        </button>
                    </div>
                </form>
            </div>
            <div style="height: 20px"></div>
            <xblock>
                <button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>
                <button class="layui-btn" onclick="x_admin_show('添加房屋','house-add.html','houseListTable')"><i
                        class="layui-icon"></i>添加
                </button>
            </xblock>

            <!-- 装载数据的容器 -->
            <div class="layui-fluid">
                <div class="layui-row">
                    <div class="layui-col-md12">
                        <table id="carTable" lay-filter="carTableFilter"></table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="/static/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    var table;
    var form;
    var $;
    layui.use(['form', 'table'], function () {
        $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        // 模糊查询
        form.on("submit(search)", function (obj) {
            console.log(obj.field);
            // let field = obj.field.field;
            // let keyword = obj.field.keyword;
            renderCarTable(obj.field);
            return false;
        });


        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            layer.alert(result, {
                title: '最终的搜索信息'
            });

            //执行搜索重载
            list.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }
            }, 'data');

            return false;
        });

        // /**
        //  * toolbar监听事件
        //  */
        // list.on('toolbar(currentTableFilter)', function (obj) {
        //     if (obj.event === 'add') {  // 监听添加操作
        //         var index = layer.open({
        //             title: '添加用户',
        //             type: 2,
        //             shade: 0.2,
        //             maxmin: true,
        //             shadeClose: true,
        //             area: ['100%', '100%'],
        //             content: '../page/table/add.html',
        //         });
        //         $(window).on("resize", function () {
        //             layer.full(index);
        //         });
        //     } else if (obj.event === 'delete') {  // 监听删除操作
        //         var checkStatus = list.checkStatus('currentTableId')
        //             , data = checkStatus.data;
        //         layer.alert(JSON.stringify(data));
        //     }
        // });

        // //监听表格复选框选择
        // list.on('checkbox(currentTableFilter)', function (obj) {
        //     console.log(obj)
        // });

        // list.on('tool(currentTableFilter)', function (obj) {
        //     var data = obj.data;
        //     if (obj.event === 'edit') {
        //
        //         var index = layer.open({
        //             title: '编辑用户',
        //             type: 2,
        //             shade: 0.2,
        //             maxmin: true,
        //             shadeClose: true,
        //             area: ['100%', '100%'],
        //             content: '../page/table/edit.html',
        //         });
        //         $(window).on("resize", function () {
        //             layer.full(index);
        //         });
        //         return false;
        //     } else if (obj.event === 'delete') {
        //         layer.confirm('真的删除行么', function (index) {
        //             obj.del();
        //             layer.close(index);
        //         });
        //     }
        // });

        // 渲染表格
        renderCarTable();

    });

    function renderCarTable(parameterJson) {

        // console.log("渲染表格")

        table.render({
            elem: '#carTable',
            url: '/car/list.do',
            where: parameterJson,
            limit: 5,
            limits: [5, 10, 15, 20],
            page: true,
            cols: [[
                {type: "checkbox", align: "center", width: 50},
                {field: 'id', width: 80, title: 'ID', sort: true},
                {
                    field: 'carBrand', width: 100, title: '品牌', templet: function (obj) {
                        return obj.carBrand == 1 ? "宝马" : "奔驰";
                    }
                },
                {
                    field: 'carType', width: 100, title: '型号', templet: function (obj) {
                        if (obj.carType == 1){
                            return "SUV";
                        } else if (obj.carType == 2){
                            return "MPV";
                        } else if(obj.carType == 3){
                            return "CDV";
                        }
                    }
                },
                {
                    field: 'productionData', width: 150, title: '生产日期', templet: function (obj) {
                        let date = new Date(obj.productionData);
                        let year = date.getFullYear();
                        let month = date.getMonth() + 1;
                        if (month < 10) {
                            month = "0" + month;
                        }
                        let day = date.getDate();
                        if (day < 10) {
                            day = "0" + day;
                        }
                        return year + "-" + month + "-" + day;
                    }
                },
                {field: 'vin', width: 150, title: 'VIN码'},
                {field: 'img', width: 150, title: '图片'},
                {
                    title: "操作", width: "180", templet: function (d) {
                        let str = "<a lay-event=\"edit\" class=\"layui-btn layui-btn-xs layui-btn-warm layui-icon\">&#xe642</a>";
                        str += " <a lay-event=\"delete\"class=\"layui-btn layui-btn-xs layui-btn-danger layui-icon\">&#xe640</a>";
                        str += " <a lay-event=\"change\"class=\"layui-btn layui-btn-xs layui-icon\">&#xe601</a>";
                        return str;
                    }
                }
            ]]
        });

    }

</script>

</body>
</html>